﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GiMess.Net;
using System.IO;
using System.Threading;
using GiMessCommons;

namespace GiMess
{
    public class GiNetClient
    {
        NetClient       netClient;
        NetMsgHandler   msgHandler;
        BinaryWriter    binWriter;

        public static GiNetClient Instance { get; private set; }

        public GiNetClient()
        {
            netClient  = new NetClient();
            msgHandler = new NetMsgHandler();         

            netClient.NewNetMsg += msgHandler.HandleMsg;

            Instance = this;

            //netClient.Connect("127.0.0.1", 1234);

            //ThreadStart clientThreadStart = netClient.Listen;
            //clientThread = new Thread(clientThreadStart);
            //clientThread.Name = "NetWorker";
            //clientThread.Start();

            //netClient.SendNetMsg(new Net.NetMsg(NetServices.SEND_MESSAGE, "message"));
            //Thread.Sleep(100);
            //SendIM("dburner", "mesajÀ€abcdÀ€mesaj");
        }

        public void Connect()
        {
            netClient.Connect(NetConsants.ConnectionIP, NetConsants.ConnectionPort);
            Thread.Sleep(100);
        }

        public void Disconnect()
        {
            netClient.Disconnect();
        }


        public void SignIn(string userName, string userPassword)
        {
            Disconnect();
            Connect();
            userPassword = NetUtils.EncodePassword(userPassword);

            DataFieldsWriter data = new DataFieldsWriter();
            data.WriteField(Field.AuthUserName, userName);
            data.WriteField(Field.AuthUserPassword, userPassword);

            netClient.SendNetMsg(NetServices.Auth, data.GetString());
            //netClient.SendNetMsg(NetServices.Auth, "abcde");
        }

        public void SignOut()
        {
            netClient.SendNetMsg(NetServices.SignOut, "");
            Disconnect();
        }

        public void SendListRequest()
        {
            netClient.SendNetMsg(NetServices.ListRequest, "");
        }

        public void ContactDelete(string username)
        {
            netClient.SendNetMsg(NetServices.ContactDelete, username);
        }

        public void ContactMove(string username, string groupname)
        {
            DataFieldsWriter writer = new DataFieldsWriter();

            writer.WriteField(Field.ListUserName,  username);
            writer.WriteField(Field.ListUserGroup, groupname);

            netClient.SendNetMsg(NetServices.ContactMove, writer.GetString());
        }

        public void GroupCreate(string groupname)
        {
            netClient.SendNetMsg(NetServices.GroupCreate, groupname);
        }

        public void SendIM(string toUserName, string text, string font)
        {
            //se inlocuieste delimitatorul cu codul lui din html
            text = text.Replace(NetConsants.FieldsDelimiter, NetConsants.FieldsDelimiterReplace);

            DataFieldsWriter data = new DataFieldsWriter();
            data.WriteField(Field.MessageUserOrigin, AuthInfo.UserName);
            data.WriteField(Field.MessageUserDest,   toUserName);
            data.WriteField(Field.MessageText,       text);
            data.WriteField(Field.MessageFont,       font);

            netClient.SendNetMsg(NetServices.SendMessage, data.GetString());
        }

        public void CreateAccount(string userName, string password, string firstName, string lastName, string email)
        {
            userName  =  userName.Replace(NetConsants.FieldsDelimiter, "");
            password  =  password.Replace(NetConsants.FieldsDelimiter, "");
            firstName = firstName.Replace(NetConsants.FieldsDelimiter, "");
            lastName  =  lastName.Replace(NetConsants.FieldsDelimiter, "");
            email     =     email.Replace(NetConsants.FieldsDelimiter, "");

            password = NetUtils.EncodePassword(password);

            DataFieldsWriter data = new DataFieldsWriter();
            data.WriteField(Field.NewAccUserName,  userName);
            data.WriteField(Field.NewAccPassword,  password);
            data.WriteField(Field.NewAccFirstName, firstName);
            data.WriteField(Field.NewAccLastName,  lastName);
            data.WriteField(Field.NewAccEmail,     email);

            netClient.SendNetMsg(NetServices.CreateAccount, data.GetString());
        }

        public void UpdateAvatar(string url)
        {
            AuthInfo.Avatar = url;

            DataFieldsWriter data = new DataFieldsWriter();
            data.WriteField(Field.UpdateAvatar, url);
            netClient.SendNetMsg(NetServices.UpdateAvatar, data.GetString());
        }

        public void UpdateStatusText(string status)
        {
            DataFieldsWriter data = new DataFieldsWriter();
            data.WriteField(Field.UpdateStatus, status);
            netClient.SendNetMsg(NetServices.UpdateStatus, data.GetString());
        }

        internal void GroupRename(string oldName, string newName)
        {
            DataFieldsWriter writer = new DataFieldsWriter();

            writer.WriteField(Field.ListUserGroup,       oldName);
            writer.WriteField(Field.ListUserGroupRename, newName);

            netClient.SendNetMsg(NetServices.GroupRename, writer.GetString());
        }

        public void ContactAdd(string username, string group)
        {
            DataFieldsWriter writer = new DataFieldsWriter();

            writer.WriteField(Field.AddUserName,  username);
            writer.WriteField(Field.AddUserGroup, group);

            netClient.SendNetMsg(NetServices.ContactAdd, writer.GetString());
        }

        public void AddRequestResponse(string username, string response)
        {
            DataFieldsWriter writer = new DataFieldsWriter();

            writer.WriteField(Field.AddUserName, username);
            writer.WriteField(Field.AddResponse, response);

            netClient.SendNetMsg(NetServices.AddRequestResponse, writer.GetString());
        }
    }
}
